package com.qfedu.sem.service.impl;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.ExampleMatcher.GenericPropertyMatchers;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.qfedu.sem.dto.BugLogDto;
import com.qfedu.sem.po.BugLog;
import com.qfedu.sem.repository.BugLogRepository;
import com.qfedu.sem.service.BugLogService;
import wo.common.entity.WoPage;
import wo.common.util.WoUtil;
import com.qfedu.sem.po.Bug;

/**
 * PO实体BugLog对应的Service接口实现类.
 * @author cailei
 */
@Service
@Transactional
public class BugLogServiceImpl implements BugLogService {

	/**
	 * 使用日志打印框架.
	 */
	private final static Logger LOG = LogManager.getLogger(BugLogServiceImpl.class);

	/**
	 * 注入BugLogRepository.
	 */
	@Resource // @Autowired
	private BugLogRepository bugLogRepository;

	/**
	 * DataTable获取故障日志列表数据.
	 * 
	 * @param start 当前页开始索引，从0开始
	 * @param length 当前页最大行数
	 * @param searchContent 查询内容
	 * @param dir 排序方式
	 * @param bugId bugId
	 * @return
	 */
	@Override
	public WoPage<BugLogDto> getPageData(Long start, Long length, String searchContent, String dir,
		java.lang.String bugId) {
		// 构造查询条件
		ExampleMatcher m = ExampleMatcher.matching();
		// 构造查询对象
		BugLog qo = new BugLog();
		// 设置条件：BugLog.bug.id = bugId
		if (!WoUtil.isEmpty(bugId)) {
			m.withMatcher("bug.id", GenericPropertyMatchers.exact());
			com.qfedu.sem.po.Bug rObj = new com.qfedu.sem.po.Bug();
			rObj.setId(bugId);
			qo.setBug(rObj);
		}
		// 设置条件：BugLog.node contains searchContent
		if (!WoUtil.isEmpty(searchContent)) {
			m.withMatcher("node", GenericPropertyMatchers.contains());
			qo.setNode(searchContent);
		}
		// 构造Example将查询条件组合起来（条件之间是逻辑与的关系）
		Example<BugLog> ex = Example.of(qo, m); 
		// 构造分页和排序对象
		Pageable pageInput = PageRequest.of(start.intValue() / length.intValue(), length.intValue());
		// 查询最终分页数据
		Page<BugLog> pageData = bugLogRepository.findAll(ex, pageInput);
		// 将PO转化为DTO
		WoPage<BugLogDto> pr = BugLogDto.getPageData(pageData.getContent(), pageData.getTotalElements());
		return pr;
	}

	/**
	 * 创建故障日志
	 * @param dto
	 */
	@Override
	public void create(BugLogDto dto) {
		BugLog po = dto.createPO();
		bugLogRepository.save(po);
	}

	/**
	 * 根据id获取故障日志数据
	 * @param id
	 * @return
	 */
	@Override
	public BugLogDto getById(String id) {
		BugLog po = bugLogRepository.findById(id).get();
		return new BugLogDto (po);
	}

	/**
	 * 修改故障日志
	 * @param dto
	 */
	@Override
	public void update(BugLogDto dto) {
		BugLog po = bugLogRepository.findById(dto.getId()).get();
		dto.updatePO(po);
	}

	/**
	 * 根据id数组删除一个或者多个故障日志
	 * @param id
	 */
	@Override
	public void delete(String[] id) {
		bugLogRepository.deleteByIdIn(id);
	}

}
